using System;
using System.Text.Json;
using System.Collections.Generic;
using JlPay.Sdk.Core;
using JlPay.Sdk.Services;

namespace JlPay.Sdk.Models
{
    // =============== 联系人信息 ===============
    
    /// <summary>
    /// 联系人信息
    /// </summary>
    public class ContactInfo
    {
        /// <summary>
        /// 联系人类型：65-经营者/法人；66-经办人
        /// </summary>
        public string? ContactType { get; set; }
        
        /// <summary>
        /// 联系人姓名（敏感信息）
        /// </summary>
        public string? ContactName { get; set; }
        
        /// <summary>
        /// 联系人手机号（敏感信息）
        /// </summary>
        public string? ContactPhone { get; set; }
        
        /// <summary>
        /// 联系人身份证号（敏感信息）
        /// </summary>
        public string? ContactIdNo { get; set; }
        
        /// <summary>
        /// 联系人邮箱（敏感信息）
        /// </summary>
        public string? ContactEmail { get; set; }
        
        /// <summary>
        /// 联系人地址（敏感信息）
        /// </summary>
        public string? ContactAddress { get; set; }
        
        /// <summary>
        /// 联系人证件有效期开始时间
        /// </summary>
        public string? ContactPeriodBegin { get; set; }
        
        /// <summary>
        /// 联系人证件有效期结束时间
        /// </summary>
        public string? ContactPeriodEnd { get; set; }
        
        /// <summary>
        /// 联系人证件正面照片
        /// </summary>
        public string? ContactIdDocCopy { get; set; }
        
        /// <summary>
        /// 联系人证件反面照片
        /// </summary>
        public string? ContactIdDocNational { get; set; }
    }

    // =============== 商户基础信息 ===============
    
    /// <summary>
    /// 商户基础信息
    /// </summary>
    public class BaseInfo
    {
        /// <summary>
        /// 申请单编号
        /// </summary>
        public string? ApplyId { get; set; }
        
        /// <summary>
        /// 产品类型：01-扫码商户；02-标准商户；03-场景商户；04-SaaS商户
        /// </summary>
        public string? ProductType { get; set; }
        
        /// <summary>
        /// 签约模式：P-线下纸质协议；E-电子签章协议；J-嘉联电子协议
        /// </summary>
        public string? SignMode { get; set; }
        
        /// <summary>
        /// 授权收款签约模式
        /// </summary>
        public string? DelegateeSignMode { get; set; }
        
        /// <summary>
        /// 纸质协议图片
        /// </summary>
        public string? AgreementPic { get; set; }
        
        /// <summary>
        /// 代理商编号
        /// </summary>
        public string? AgentId { get; set; }
        
        /// <summary>
        /// 是否自动生成码付终端号
        /// </summary>
        public string? QrcodeTermAuto { get; set; }
        
        /// <summary>
        /// 联系人信息
        /// </summary>
        public ContactInfo? ContactInfo { get; set; }
    }

    // =============== 营业执照信息 ===============
    
    /// <summary>
    /// 营业执照信息
    /// </summary>
    public class LicenseInfo
    {
        /// <summary>
        /// 主体类型
        /// </summary>
        public string? LicenseType { get; set; }
        
        /// <summary>
        /// 营业执照号码（敏感信息）
        /// </summary>
        public string? LicenseNo { get; set; }
        
        /// <summary>
        /// 证书编号
        /// </summary>
        public string? CertNumber { get; set; }
        
        /// <summary>
        /// 营业执照上的名称（敏感信息）
        /// </summary>
        public string? LicenseName { get; set; }
        
        /// <summary>
        /// 登记证书地址
        /// </summary>
        public string? LicenseAddress { get; set; }
        
        /// <summary>
        /// 营业执照注册开始日期
        /// </summary>
        public string? PeriodBegin { get; set; }
        
        /// <summary>
        /// 营业执照注册结束日期
        /// </summary>
        public string? PeriodEnd { get; set; }
        
        /// <summary>
        /// 营业执照照片
        /// </summary>
        public string? LicensePic { get; set; }
        
        /// <summary>
        /// 经营范围
        /// </summary>
        public string? BusinessScope { get; set; }
    }

    // =============== 身份信息 ===============
    
    /// <summary>
    /// 身份信息
    /// </summary>
    public class IdentityInfo
    {
        /// <summary>
        /// 证件类型
        /// </summary>
        public string? IdDocType { get; set; }
        
        /// <summary>
        /// 经营者/法人的证件姓名（敏感信息）
        /// </summary>
        public string? IdCardName { get; set; }
        
        /// <summary>
        /// 经营者/法人的证件英文名称（敏感信息）
        /// </summary>
        public string? IdCardEnglishName { get; set; }
        
        /// <summary>
        /// 经营者/法人的证件号码（敏感信息）
        /// </summary>
        public string? IdCardNo { get; set; }
        
        /// <summary>
        /// 经营者/法人的手机号（敏感信息）
        /// </summary>
        public string? LegalPersonPhone { get; set; }
        
        /// <summary>
        /// 身份证件有效期开始时间
        /// </summary>
        public string? CardPeriodBegin { get; set; }
        
        /// <summary>
        /// 身份证件有效期结束时间
        /// </summary>
        public string? CardPeriodEnd { get; set; }
        
        /// <summary>
        /// 证件上住址（敏感信息）
        /// </summary>
        public string? IdCardAddress { get; set; }
        
        /// <summary>
        /// 证件正面照片
        /// </summary>
        public string? IdCardCopy { get; set; }
        
        /// <summary>
        /// 证件反面照片
        /// </summary>
        public string? IdCardNational { get; set; }
        
        /// <summary>
        /// 性别
        /// </summary>
        public string? Gender { get; set; }
        
        /// <summary>
        /// 国籍
        /// </summary>
        public string? Nationality { get; set; }
    }

    // =============== 商户身份信息 ===============
    
    /// <summary>
    /// 商户身份信息
    /// </summary>
    public class IdentificationInfo
    {
        /// <summary>
        /// 主体类型：0-小微；1-营业执照、政府机关、事业单位或其他组织
        /// </summary>
        public string? MerchType { get; set; }
        
        /// <summary>
        /// 营业执照信息
        /// </summary>
        public LicenseInfo? LicenseInfo { get; set; }
        
        /// <summary>
        /// 身份信息
        /// </summary>
        public IdentityInfo? IdentityInfo { get; set; }
    }

    // =============== 商户经营信息 ===============
    
    /// <summary>
    /// 商户经营信息
    /// </summary>
    public class BusinessInfo
    {
        /// <summary>
        /// 商户经营名称
        /// </summary>
        public string? MerchShortname { get; set; }
        
        /// <summary>
        /// 商户英文名称
        /// </summary>
        public string? MerchEnglishName { get; set; }
        
        /// <summary>
        /// MCC码
        /// </summary>
        public string? MccCode { get; set; }
        
        /// <summary>
        /// 业务场景编号
        /// </summary>
        public string? SceneId { get; set; }
        
        /// <summary>
        /// 行政县/区码
        /// </summary>
        public string? AddressCode { get; set; }
        
        /// <summary>
        /// 商户实际营业场所的详细地址
        /// </summary>
        public string? AddressDetail { get; set; }
        
        /// <summary>
        /// 门头照片
        /// </summary>
        public string? DoorPic { get; set; }
        
        /// <summary>
        /// 店内照片
        /// </summary>
        public string? IndoorPic { get; set; }
        
        /// <summary>
        /// 收银台照片
        /// </summary>
        public string? CashierPic { get; set; }
    }

    // =============== 商户进件请求 ===============
    
    /// <summary>
    /// 商户进件请求
    /// </summary>
    public class MerchantIncomingAddRequest : OrgBaseRequest
    {
        /// <summary>
        /// 商户基础信息
        /// </summary>
        public BaseInfo? BaseInfo { get; set; }
        
        /// <summary>
        /// 商户身份信息
        /// </summary>
        public IdentificationInfo? IdentificationInfo { get; set; }
        
        /// <summary>
        /// 商户经营信息
        /// </summary>
        public BusinessInfo? BusinessInfo { get; set; }

        /// <summary>
        /// 构造函数，设置默认敏感字段
        /// </summary>
        public MerchantIncomingAddRequest()
        {
            // 联系人信息
            AddSensitiveField("base_info.contact_info.contact_name");
            AddSensitiveField("base_info.contact_info.contact_phone");
            AddSensitiveField("base_info.contact_info.contact_id_no");
            AddSensitiveField("base_info.contact_info.contact_email");
            AddSensitiveField("base_info.contact_info.contact_address");

            // 证件信息
            AddSensitiveField("identification_info.identity_info.id_card_name");
            AddSensitiveField("identification_info.identity_info.id_card_no");
            AddSensitiveField("identification_info.identity_info.legal_person_phone");
            AddSensitiveField("identification_info.identity_info.id_card_address");

            // 营业执照信息
            AddSensitiveField("identification_info.license_info.license_name");
            AddSensitiveField("identification_info.license_info.license_no");

            // 结算账户信息
            AddSensitiveField("settlement_info.account_info.account_no");
            AddSensitiveField("settlement_info.account_info.account_name");
            AddSensitiveField("settlement_info.account_info.bank_set_phone");
            AddSensitiveField("settlement_info.account_info.delegatee_name");
            AddSensitiveField("settlement_info.account_info.delegatee_id_card");
            AddSensitiveField("settlement_info.account_info.delegatee_phone");

            // 其他补充敏感字段
            AddSensitiveField("additional_info.email");
            AddSensitiveField("additional_info.contact_address");
            AddSensitiveField("additional_info.emergency_contact_name");
            AddSensitiveField("additional_info.emergency_contact_phone");
        }
    }

    /// <summary>
    /// 商户进件响应
    /// </summary>
    public class MerchantIncomingAddResponse : StandardResponse
    {
        /// <summary>
        /// 申请单编号
        /// </summary>
        public string? ApplyId { get; set; }
    }

    // =============== 商户进件修改 ===============
    
    /// <summary>
    /// 商户进件修改请求
    /// </summary>
    public class MerchantIncomingModifyRequest : OrgBaseRequest
    {
        /// <summary>
        /// 商户基础信息
        /// </summary>
        public BaseInfo? BaseInfo { get; set; }
        
        /// <summary>
        /// 商户身份信息
        /// </summary>
        public IdentificationInfo? IdentificationInfo { get; set; }
        
        /// <summary>
        /// 商户经营信息
        /// </summary>
        public BusinessInfo? BusinessInfo { get; set; }

        /// <summary>
        /// 构造函数，设置默认敏感字段
        /// </summary>
        public MerchantIncomingModifyRequest()
        {
            // 添加与进件请求相同的敏感字段
            AddSensitiveField("base_info.contact_info.contact_name");
            AddSensitiveField("base_info.contact_info.contact_phone");
            AddSensitiveField("base_info.contact_info.contact_id_no");
            AddSensitiveField("base_info.contact_info.contact_email");
            AddSensitiveField("base_info.contact_info.contact_address");

            AddSensitiveField("identification_info.identity_info.id_card_name");
            AddSensitiveField("identification_info.identity_info.id_card_no");
            AddSensitiveField("identification_info.identity_info.legal_person_phone");
            AddSensitiveField("identification_info.identity_info.id_card_address");

            AddSensitiveField("identification_info.license_info.license_name");
            AddSensitiveField("identification_info.license_info.license_no");

            AddSensitiveField("settlement_info.account_info.account_no");
            AddSensitiveField("settlement_info.account_info.account_name");
            AddSensitiveField("settlement_info.account_info.bank_set_phone");
            AddSensitiveField("settlement_info.account_info.delegatee_name");
            AddSensitiveField("settlement_info.account_info.delegatee_id_card");
            AddSensitiveField("settlement_info.account_info.delegatee_phone");

            AddSensitiveField("additional_info.email");
            AddSensitiveField("additional_info.contact_address");
            AddSensitiveField("additional_info.emergency_contact_name");
            AddSensitiveField("additional_info.emergency_contact_phone");
        }
    }

    /// <summary>
    /// 商户进件修改响应
    /// </summary>
    public class MerchantIncomingModifyResponse : StandardResponse
    {
        /// <summary>
        /// 申请单编号
        /// </summary>
        public string? ApplyId { get; set; }
    }

    // =============== 实名认证查询 ===============
    
    /// <summary>
    /// 实名认证查询请求
    /// </summary>
    public class RealnameQueryRequest : OrgBaseRequest, IRequestMapper
    {
        /// <summary>
        /// 查询类型：0-申请单查询（默认）；1-授权结果查询
        /// </summary>
        public string QueryType { get; set; } = "0";
        
        /// <summary>
        /// 渠道类型：00-所有渠道(默认)；01-wechat；02-alipay
        /// </summary>
        public string ChannelType { get; set; } = "00";

        /// <summary>
        /// 转换为字典
        /// </summary>
        public Dictionary<string, object> ToMap()
        {
            var result = new Dictionary<string, object>();

            // 添加基类字段
            if (!string.IsNullOrEmpty(OrgNo))
                result["org_no"] = OrgNo;
            if (!string.IsNullOrEmpty(MerchNo))
                result["merch_no"] = MerchNo;

            // 添加本类字段
            if (!string.IsNullOrEmpty(QueryType))
                result["query_type"] = QueryType;
            if (!string.IsNullOrEmpty(ChannelType))
                result["channel_type"] = ChannelType;

            return result;
        }
    }

    /// <summary>
    /// 实名认证信息
    /// </summary>
    public class RealnameInfo
    {
        /// <summary>
        /// 渠道类型：01-wechat；02-alipay
        /// </summary>
        public string? ChannelType { get; set; }
        
        /// <summary>
        /// 申请状态
        /// </summary>
        public string? ApplymentStatus { get; set; }
        
        /// <summary>
        /// 二维码URL地址
        /// </summary>
        public string? QrcodeData { get; set; }
        
        /// <summary>
        /// 驳回的字段名
        /// </summary>
        public string? RejectParam { get; set; }
        
        /// <summary>
        /// 实名认证驳回原因
        /// </summary>
        public string? RejectReason { get; set; }
        
        /// <summary>
        /// 授权状态：200-已授权；201-未授权
        /// </summary>
        public string? AuthorizeState { get; set; }
    }

    /// <summary>
    /// 实名认证查询响应
    /// </summary>
    public class RealnameQueryResponse : StandardResponse
    {
        /// <summary>
        /// 认证信息
        /// </summary>
        public List<RealnameInfo>? RealnameInfo { get; set; }
    }
} 